package com.socool.sinofaith.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import net.tsz.afinal.FinalBitmap;

import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.util.ConstansJsonUrl;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class FullscreenShowActivity extends Activity {
	
	private String images,type;
	
	final int IMAGE_MAX_SIZE = 1024;
	
	private ImageView fullImage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_fullscreen_show);
		SocoolApplication.addActivity(this);
		fullImage=(ImageView) findViewById(R.id.fullImage);
		images=getIntent().getStringExtra("images");
		type=getIntent().getStringExtra("type");
		if(null!=type && "0".equals(type)){
			fullImage.setImageBitmap(getBitmap(images));
		}else{
			Log.v("qqqq", images);
			FinalBitmap finalBitmap = new FinalBitmap(
					FullscreenShowActivity.this).init();
			finalBitmap.display(
					fullImage,images);
		}
		fullImage.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				FullscreenShowActivity.this.finish();
			}
		});
	}
	
	private Uri getImageUri(String path) {

		return Uri.fromFile(new File(path));
	}

	private Bitmap getBitmap(String path) {

		Uri uri = getImageUri(path);
		InputStream in = null;
		try {
			in = getContentResolver().openInputStream(uri);

			// Decode image size
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;

			BitmapFactory.decodeStream(in, null, o);
			in.close();

			int scale = 1;
			if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
				scale = (int) Math.pow(
						2,
						(int) Math.round(Math.log(IMAGE_MAX_SIZE
								/ (double) Math.max(o.outHeight, o.outWidth))
								/ Math.log(0.5)));
			}

			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			in = getContentResolver().openInputStream(uri);
			Bitmap b = BitmapFactory.decodeStream(in, null, o2);
			in.close();

			return b;
		} catch (FileNotFoundException e) {
			// Log.e(TAG, "file " + path + " not found");
		} catch (IOException e) {
			// Log.e(TAG, "file " + path + " not found");
		}
		return null;
	}
}
